from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import Qt
import sys, random


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 500, 300)
        self.setWindowTitle('Points')
        self.show()

    def paintEvent(self, e):
        qp = QPainter()
        qp.begin(self)
        self.drawPoints(qp)
        qp.end()

    def drawPoints(self, qp):
        # 设置画笔颜色为红色
        qp.setPen(Qt.red)
        # 获取当前窗口的大小
        size = self.size()

        # 如果窗口的高度或宽度小于等于1，则不进行绘制
        if size.height() <= 1 or size.width() <= 1:
            return

        # 循环1000次，随机生成1000个点
        for i in range(1000):
            # 随机生成点的x坐标，范围在1到窗口宽度-1之间
            x = random.randint(1, size.width() - 1)
            # 随机生成点的y坐标，范围在1到窗口高度-1之间
            y = random.randint(1, size.height() - 1)
            # 在生成的坐标处绘制一个点
            qp.drawPoint(x, y)


def main():
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()